import werkzeug
from flask import Flask, render_template, jsonify

from app import app


# 自定义一个 HTTPException
class CustomException(werkzeug.exceptions.HTTPException):

    def __init__(self, code, description):
        self.code = code
        self.description = description


@app.errorhandler(CustomException)
def custom_exception_handler(e):
    return render_template('errors/error.html', error=e), e.code


@app.errorhandler(401)
def bad_request(e):
    return render_template('errors/error.html', error=e), 401


@app.errorhandler(403)
def bad_request(e):
    return render_template('errors/error.html', error=e), 403


@app.errorhandler(404)
def bad_request(e):
    return render_template('errors/error.html', error=e), 404


@app.errorhandler(410)
def bad_request(e):
    return render_template('errors/error.html', error=e), 410
    # Returning API errors as JSON
    # return jsonify(error=str(e)), 410


@app.errorhandler(500)
def bad_request(e):
    e.description = 'Internal Server Error!'
    return render_template('errors/error.html', error=e), 500
